package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.MarketFootPrintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.model.admin.vo.BaseData;
import com.cskaoyan.model.vx.vo.WxFootprintVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: 牛犇
 * @date 2021/12/2 15:56
 */
@Service
public class WxFootPrintServiceImpl implements WxFootprintService {

    @Autowired
    MarketFootPrintMapper marketFootPrintMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public BaseData selectFootprintList(Integer page, Integer limit,Integer userId) {
        PageHelper.startPage(page,limit);
        MarketFootPrintExample example=new MarketFootPrintExample();
        example.createCriteria().andUserIdEqualTo(userId);
        example.setOrderByClause("id desc");
        List<MarketFootPrint> marketFootPrints = marketFootPrintMapper.selectByExample(example);
        List<WxFootprintVo> wxFootprintVos=new ArrayList<>();
        for (MarketFootPrint marketFootPrint : marketFootPrints) {
            MarketGoodsExample example1=new MarketGoodsExample();
            example1.createCriteria().andIdEqualTo(marketFootPrint.getGoodsId());
            List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExample(example1);
            MarketGoods marketGoods=marketGoodsList.get(0);
            wxFootprintVos.add(new WxFootprintVo(marketGoods.getBrief(),marketGoods.getPicUrl(),marketGoods.getAddTime(),marketGoods.getId(),marketGoods.getName(),marketGoods.getId(),marketGoods.getRetailPrice()));
        }
        PageInfo info = new PageInfo(wxFootprintVos);
        int total = (int) info.getTotal();
        BaseData data = BaseData.list(wxFootprintVos, total, page, limit, total / limit);
        return data;
    }

    @Override
    public void delete(int id) {
        marketFootPrintMapper.deleteByPrimaryKey(id);
    }
}
